package com.sense.kukoo.common.wiget.dialog

import android.app.Dialog
import android.content.Context
import android.content.DialogInterface
import android.os.Build
import android.os.Bundle
import android.view.View
import android.widget.TextView
import com.sense.kukoo.common.R
import com.sense.kukoo.common.anim.BottomMotionAnimHelper
import com.sense.kukoo.common.media.Kukoo
import com.sense.kukoo.common.media.bean.LocalMediaListItemBean
import com.sense.kukoo.common.media.timer.TimerImpl
import com.sense.kukoo.common.media.watcher.UserWatcher
import com.sense.kukoo.common.utils.ApplicationContext
import com.sense.kukoo.common.wiget.MediaSettingTabLayout
import com.sense.kukoo.lib_logger.LoggerManager

class MediaSettingDialog: Dialog {

    private val TAG: String = javaClass.simpleName

    constructor(context: Context):super(context)

    constructor(context: Context, themeResId: Int):super(context, themeResId)

    constructor(context: Context, cancelable: Boolean, cancelListener: DialogInterface.OnCancelListener?) : super(context, cancelable, cancelListener)

    private val mBottomMotionAnimHelper = BottomMotionAnimHelper()

    private lateinit var tab_media_setting_speed: MediaSettingTabLayout
    private lateinit var tab_media_setting_timer: MediaSettingTabLayout
    private lateinit var tv_playback_content: TextView
    private lateinit var tv_timer_content: TextView

    private lateinit var speeds:Array<String>
    private lateinit var timers:Array<String>

    private var mSpeed = -1F
    private var mTimerType = -1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        context.setTheme(R.style.bottom_dialog)
        setContentView(R.layout.common_dialog_media_setting)
        //
        val iv_btn_close = findViewById<View>(R.id.iv_btn_close)
        iv_btn_close.setOnClickListener {
            dismiss()
        }
        val fl_setting_root = findViewById<View>(R.id.fl_setting_root)
        val cl_setting_content = findViewById<View>(R.id.cl_setting_content)
        val first = ApplicationContext.dp2px(344F).toFloat()
        mBottomMotionAnimHelper.setAnimateDuration(200L)
        mBottomMotionAnimHelper.init(fl_setting_root, cl_setting_content, first){
            _dismiss()
        }
        //
        tv_playback_content = findViewById<TextView>(R.id.tv_playback_content)
        tv_timer_content = findViewById<TextView>(R.id.tv_timer_content)
        //
        speeds = context.resources.getStringArray(com.sense.kukoo.common_i18n.R.array.array_music_speed)
        timers =  context.resources.getStringArray(com.sense.kukoo.common_i18n.R.array.array_music_timer)
        //
        tab_media_setting_speed = findViewById<MediaSettingTabLayout>(R.id.tab_media_setting_speed)
        tab_media_setting_speed.init(speeds)
        tab_media_setting_speed.setOnTabListener(object :MediaSettingTabLayout.OnTabListener{
            override fun onTabListener(position: Int) {
                LoggerManager.d(TAG, "speed position=${position}, speed=${speeds[position]}, mSpeed=${mSpeed}")
                var newSpeed: Float = -1F
                when (position) {
                    0 -> {
                        newSpeed = UserWatcher.MusicStateWatcher.SPEED_0P8
                    }
                    1 -> {
                        newSpeed = UserWatcher.MusicStateWatcher.SPEED_1P0
                    }
                    2 -> {
                        newSpeed = UserWatcher.MusicStateWatcher.SPEED_1P2
                    }
                    3 -> {
                        newSpeed = UserWatcher.MusicStateWatcher.SPEED_1P5
                    }
                    4 -> {
                        newSpeed = UserWatcher.MusicStateWatcher.SPEED_2P0
                    }
                }
                if(mSpeed != newSpeed){
                    mSpeed = newSpeed
                    tv_playback_content.text = speeds[position]
                    //版本适配
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
                        Kukoo.input.setSpeed(newSpeed)
                    }
                }
            }
        })

        tab_media_setting_timer = findViewById<MediaSettingTabLayout>(R.id.tab_media_setting_timer)
        tab_media_setting_timer.init(timers)
        tab_media_setting_timer.setOnTabListener(object :MediaSettingTabLayout.OnTabListener{
            override fun onTabListener(position: Int) {
                LoggerManager.d(TAG, "timer position=${position}, timers=${timers[position]}")
                var newTimerType = -1
                when (position) {
                    0 -> {
                        newTimerType = TimerImpl.TYPE_OFF
                    }
                    1 -> {
                        newTimerType = TimerImpl.TYPE_15_MIN
                    }
                    2 -> {
                        newTimerType = TimerImpl.TYPE_30_MIN
                    }
                    3 -> {
                        newTimerType = TimerImpl.TYPE_60_MIN
                    }
                    4 -> {
                        newTimerType = TimerImpl.TYPE_90_MIN
                    }
                }
                if(mTimerType != newTimerType){
                    mTimerType = newTimerType
                    Kukoo.input.setTimer(mTimerType)
                }
            }
        })
    }

    override fun show() {
        super.show()
        mBottomMotionAnimHelper.animateToStart()
        Kukoo.watcher.register(mMusicListener)
        Kukoo.watcher.registerTimerWatcher(mTimerlistener)
    }

    override fun dismiss() {
        Kukoo.watcher.unregister(mMusicListener)
        Kukoo.watcher.unregisterTimerWatcher(mTimerlistener)
        mBottomMotionAnimHelper.animateToFinish()
    }

    private fun _dismiss() {
        super.dismiss()
    }

    private val mMusicListener = object : UserWatcher.MusicStateWatcher{
        override fun onMusicChanged(music: LocalMediaListItemBean) {}

        override fun onPlayTimeChanged(currentInt: Int, totalInt: Int, current: String, total: String) {}

        override fun onPlayPercentChanged(percent: Int) {}

        override fun onPlayStateChanged(state: Int) {}

        override fun onPlaySpeedChanged(speed: Float) {
            LoggerManager.d(TAG, "onPlaySpeedChanged() mSpeed=${mSpeed}, speed${speed}")
            if(mSpeed != speed){
                mSpeed = speed
                when (speed) {
                    UserWatcher.MusicStateWatcher.SPEED_0P8 -> {
                        tab_media_setting_speed.selectTab(tab_media_setting_speed.getTabAt(0))
                        tv_playback_content.text = speeds[0]
                    }
                    UserWatcher.MusicStateWatcher.SPEED_1P0 -> {
                        tab_media_setting_speed.selectTab(tab_media_setting_speed.getTabAt(1))
                        tv_playback_content.text = speeds[1]
                    }
                    UserWatcher.MusicStateWatcher.SPEED_1P2 -> {
                        tab_media_setting_speed.selectTab(tab_media_setting_speed.getTabAt(2))
                        tv_playback_content.text = speeds[2]
                    }
                    UserWatcher.MusicStateWatcher.SPEED_1P5 -> {
                        tab_media_setting_speed.selectTab(tab_media_setting_speed.getTabAt(3))
                        tv_playback_content.text = speeds[3]
                    }
                    UserWatcher.MusicStateWatcher.SPEED_2P0 -> {
                        tab_media_setting_speed.selectTab(tab_media_setting_speed.getTabAt(4))
                        tv_playback_content.text = speeds[4]
                    }
                }
            }

        }

        override fun onPlayListChanged() {}
    }

    private val mTimerlistener = object : UserWatcher.TimerStateWatcher{
        override fun onTimerChanged(type: Int, left: Long, leftStr: String) {
            LoggerManager.d(TAG, "onTimerChanged() type=${type}, left=${left}, leftStr=${leftStr}")
            if(mTimerType != type){
                mTimerType = type
                when (type) {
                    TimerImpl.TYPE_OFF -> {
                        tab_media_setting_timer.selectTab(tab_media_setting_timer.getTabAt(0))
                    }
                    TimerImpl.TYPE_15_MIN -> {
                        tab_media_setting_timer.selectTab(tab_media_setting_timer.getTabAt(1))
                    }
                    TimerImpl.TYPE_30_MIN -> {
                        tab_media_setting_timer.selectTab(tab_media_setting_timer.getTabAt(2))
                    }
                    TimerImpl.TYPE_60_MIN -> {
                        tab_media_setting_timer.selectTab(tab_media_setting_timer.getTabAt(3))
                    }
                    TimerImpl.TYPE_90_MIN -> {
                        tab_media_setting_timer.selectTab(tab_media_setting_timer.getTabAt(4))
                    }
                }
            }
            //
            if(type == TimerImpl.TYPE_OFF){
                tv_timer_content.text = timers[0]
            } else {
                tv_timer_content.text = leftStr
            }
        }
    }

}